
//     描述：实现 UTF-8 格式的 BASE64 编解码
// 注意事项：如果是其它格式的要转换为 UTF-8 格式
// 使用方法：编码调用 encode_base64 函数, 解码调用 decode_base64 函数，具体见最后参考函数

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef unsigned char u_char;

/**********************************************************
 * 描述: Base64编码后字符长度
 * 输入: 编码前长度
 * 返回: 编码后长度
 * ********************************************************/
 
inline int base64_encoded_length(int len)
{
	return (((len + 2) / 3) * 4);
}

/**********************************************************
 * 描述: Base64解码后字符长度
 * 输入: 编码前长度
 * 返回: 编码后长度
 * ********************************************************/
 
inline int base64_decoded_length(int len)
{
	return (((len + 3) / 4) * 3);
}

typedef struct {
    size_t      len;
    u_char     *data;
} str_t;

/**********************************************************
 * 描述: Base64 编码函数
 * 输入: dst	目的结构体
		 src	原结构体
 * 返回: 空
 * ********************************************************/
 
void encode_base64(str_t *dst, str_t *src)
{
    size_t          len = 0;
    u_char			*d = NULL;
	u_char			*s = NULL;
    static u_char   basis64[] =
            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

    len = src->len;
    s = src->data;
    d = dst->data;

    while (len > 2)
	{
        *d++ = basis64[(s[0] >> 2) & 0x3f];
        *d++ = basis64[((s[0] & 3) << 4) | (s[1] >> 4)];
        *d++ = basis64[((s[1] & 0x0f) << 2) | (s[2] >> 6)];
        *d++ = basis64[s[2] & 0x3f];

        s += 3;
        len -= 3;
    }

    if (len)
	{
        *d++ = basis64[(s[0] >> 2) & 0x3f];

        if (len == 1)
		{
            *d++ = basis64[(s[0] & 3) << 4];
            *d++ = '=';
        }
		else
		{
            *d++ = basis64[((s[0] & 3) << 4) | (s[1] >> 4)];
            *d++ = basis64[(s[1] & 0x0f) << 2];
        }

        *d++ = '=';
    }

    dst->len = d - dst->data;
}

/**********************************************************
 * 描述: 实际的解码函数
 * 输入: basis 解码字符数组, 在 decode_base64 函数中定义
 * 返回: 0: 成功; -1: 错误
 * ********************************************************/
 
static int decode_base64_internal(str_t *dst, str_t *src, const u_char *basis)
{
    size_t	len = 0;
    u_char	*d = NULL;
	u_char	*s = NULL;;

    for (len = 0; len < src->len; len++)
	{
        if (src->data[len] == '=')
		{
            break;
        }

        if (basis[src->data[len]] == 77)
		{
            return -1;
        }
    }

    if (len % 4 == 1)
	{
        return -1;
    }

    s = src->data;
    d = dst->data;

    while (len > 3)
	{
        *d++ = (u_char)(basis[s[0]] << 2 | basis[s[1]] >> 4);
        *d++ = (u_char)(basis[s[1]] << 4 | basis[s[2]] >> 2);
        *d++ = (u_char)(basis[s[2]] << 6 | basis[s[3]]);

        s += 4;
        len -= 4;
    }

    if (len > 1)
	{
        *d++ = (u_char) (basis[s[0]] << 2 | basis[s[1]] >> 4);
    }

    if (len > 2)
	{
        *d++ = (u_char) (basis[s[1]] << 4 | basis[s[2]] >> 2);
    }

    dst->len = d - dst->data;

    return 0;
}

/**********************************************************
 * 描述: Base64 解码函数
 * 输入: dst	目的结构体
         src	原结构体
 * 返回: 0: 成功; -1: 错误
 * ********************************************************/
 
int decode_base64(str_t *dst, str_t *src)
{
    static u_char basis64[] = {
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 62, 77, 77, 77, 63,
        52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 77, 77, 77, 77, 77, 77,
        77,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
        15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 77, 77, 77, 77, 77,
        77, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 77, 77, 77, 77, 77,

        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77,
        77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77, 77
    };

    return decode_base64_internal(dst, src, basis64);
}

int main(int argc, char **argv)
{
	char buff[] = "四川名山";
	int len = strlen(buff);
	str_t t;
	t.data = (u_char *)malloc(20);
	memset(t.data, 0, 20);
	strcpy((char *)t.data, buff);
	t.len = len;

	printf("len = %d\n", base64_encoded_length(t.len));
	str_t s;
	s.data = (u_char *)malloc(20);
	memset(s.data, 0, 20);
	s.len = 0;

	encode_base64(&s, &t);
	printf("encode: %s\n", s.data);
	printf("len: %d\n", s.len);

	printf("len = %d\n", base64_decoded_length(s.len));
	str_t p;
	p.data = (u_char *)malloc(20);
	decode_base64(&p, &s);
	printf("decode: %s\n", p.data);
	printf("len: %d\n", p.len);

	free(t.data);
	t.data = NULL;
	free(p.data);
	p.data = NULL;

    return 0;
}

